/*
	Table 3 and Table 4
	Differences in means tests for Study 1 and Study 2 respectively.
	The code for the regressions here is the same as the code used to produce 
	the main figures and tables.
*/

// Study 1 

use data_study_1.dta, clear

// Set omitted categories
	gen zero_race = 0
	label var zero_race "White"
	gen zero_sex = 0
	label var zero_sex "Male"
	gen zero_eco = 0
	label var zero_eco "Maintain investment in energy"
	
// Set regression variables
	loc reg_race "cand_black zero_race"
	loc reg_sex "cand_female zero_sex"
	loc reg_issues "cand_policy_abort1 cand_policy_abort2 cand_policy_tax1 cand_policy_tax2"
	loc reg_issues "`reg_issues' cand_policy_health1 cand_policy_health2 cand_policy_eco1 zero_eco"
	loc reg_affirm "cand_policy_aa1 cand_policy_aa2 cand_policy_aa3"
	
// Interactive Terms for Black Candidate X Affirmative Action
	gen noracepolicy = cand_policy_aa1 == 0 & cand_policy_aa2 == 0 & cand_policy_aa3 == 0	
	gen black_aa1 = cand_policy_aa1*cand_black
	gen black_aa2 = cand_policy_aa2*cand_black
	gen black_aa3 = cand_policy_aa3*cand_black
	gen white_aa1 = cand_policy_aa1*cand_white
	gen white_aa2 = cand_policy_aa2*cand_white
	gen white_aa3 = cand_policy_aa3*cand_white
	gen black_aa0 = noracepolicy*cand_black
	gen white_aa0 = noracepolicy*cand_white
	
	replace white_aa0 = 0
	
	loc int "white_aa0 white_aa1 white_aa2 white_aa3 black_aa0 black_aa1 black_aa2 black_aa3"
	
	gen out_fair_bwdiff = out_fair_black - out_fair_white
		
	foreach var in out_ideo_7 out_priority_sj out_fair_bwdiff	{
		loc n = subinstr("`var'", "out_", "", .)
		loc n = subinstr("`n'", "_7", "", .)
		loc n = subinstr("`n'", "priority_", "", .)
		loc n = subinstr("`n'", "_bwdiff", "", .)
		
		// Manually create table of differences
		tempname t_`n'
		tempfile tf_`n'
		postfile `t_`n'' str20 var `n'_coef `n'_se using `tf_`n''
			
			reg `var' `reg_sex' `reg_issues' `int' cand_age, robust
			
			lincom black_aa0 - white_aa0
			post `t_`n'' ("BW, No AA") (r(estimate)) (r(se))
			
			lincom black_aa3 - white_aa0
			post `t_`n'' ("B Cons - W No AA") (r(estimate)) (r(se))
			
			lincom black_aa1 - white_aa1
			post `t_`n'' ("BW, Lib") (r(estimate)) (r(se))
			
			lincom black_aa3 - white_aa3
			post `t_`n'' ("BW, Cons") (r(estimate)) (r(se))
		
		postclose `t_`n''
		
		preserve
			use `tf_`n'', clear
			list
				
			reshape long `n'_, i(var) j(stat) s 
				
			save `tf_`n'', replace
		restore	
	}

	use `tf_ideo', clear
	
	cap drop _m
	merge 1:1 var stat using `tf_sj'
	drop _m
	merge 1:1 var stat using `tf_fair'
	drop _m
	list
	
	foreach v in ideo sj fair {
		gen `v' = "0"+string(round(`v'_, 0.002))
		replace `v' = subinstr(`v', "-","", .)
		replace `v' = "-"+`v' if substr(string(`v'_), 1, 1) == "-"
		
		drop `v'_
	}
	
	gen order = . 
	replace order = 1 if var == "BW, No AA"
	replace order = 2 if var == "B Cons - W No AA"
	replace order = 3 if var == "BW, Cons"
	replace order = 4 if var == "BW, Lib"
	replace order = order + 0.5 if stat == "se"
	sort order
	
	
	gen varname = ""
	replace varname = "Black - White, No Position" if order == 1
	replace varname = "Back, Replace - White, No Position" if order == 2
	replace varname = "Black - White, Replace" if order == 3
	replace varname = "Black - White, Expand" if order == 4
	
	drop order var 
	order var stat varname ideo sj fair
	list
	
	outsheet using table_3.csv, comma replace
		
	


// Study 2
	use data_study_2.dta, clear


// Set omitted categories
	gen zero_race = 0
	label var zero_race "White"
	gen zero_sex = 0
	label var zero_sex "Male"
	gen zero_eco = 0
	label var zero_eco "Maintain investment in energy"
	gen zero_biden = 0
	label var zero_biden "Vote Share: 51%"
	gen zero_exp = 0
	label var zero_exp "Political newcomer"
	gen zero_dist = 0
	label var zero_dist "[63, 8, 13, 11, 5]"
	gen zero_racepol = 0
	label var zero_racepol "Not shown policy"
	
// Set regression variables
	loc reg_race "cand_black zero_race cand_asian cand_hispa"
	loc reg_sex "cand_female zero_sex"
	loc reg_exp "cand_exp_teach cand_exp_council cand_exp_lawyer cand_exp_business zero_exp"
	loc reg_biden "cand_biden_p59 cand_biden_p57 cand_biden_p55 cand_biden_p53 zero_biden"
	loc reg_distpop "cand_dist1 cand_dist2 cand_dist3 cand_dist4 cand_dist5 cand_dist6 zero_dist"
	loc reg_issues "cand_policy_abort1 cand_policy_abort2 cand_policy_tax1 cand_policy_tax2"
	loc reg_issues "`reg_issues' cand_policy_health1 cand_policy_health2 cand_policy_eco1 zero_eco"
	loc reg_affirm "cand_policy_aa1 cand_policy_aa2 cand_policy_aa3 zero_racepol"
	loc reg_affirm2 "noracepolicy cand_policy_aa1 cand_policy_aa2 cand_policy_aa3 "
	
// Interactive Terms for Black Candidate X Affirmative Action
	gen noracepolicy = cand_policy_aa1 == 0 & cand_policy_aa2 == 0 & cand_policy_aa3 == 0	
	
	loc int ""
	foreach r in white black asian hispa {
		gen `r'_aa1 = cand_policy_aa1*cand_`r'
		gen `r'_aa2 = cand_policy_aa2*cand_`r'
		gen `r'_aa3 = cand_policy_aa3*cand_`r'
		gen `r'_aa0 = noracepolicy*cand_`r'
			
		loc int "`int' `r'_aa0 `r'_aa1 `r'_aa2 `r'_aa3"
	
	}
	// set reference
	replace white_aa0 = 0
	
	gen out_fair_bwdiff = out_fair_black - out_fair_white
	
		
//============================================================================== Regress the things

	// Ideology
	tempname temp
	tempfile tempf
	postfile `temp' str20 var ideo_coef ideo_se using `tempf'
	forvalues var = 0/3 {
		reg out_ideo `reg_sex' `reg_exp' `reg_biden' `reg_distpop' `reg_issues' `int' ///
			cand_age, vce(cluster r_id)
		
		lincom _cons + black_aa`var' 
		post `temp' ("black_aa`var'") (r(estimate)) (r(se))
		
		lincom _cons + white_aa`var' 
		post `temp' ("white_aa`var'") (r(estimate)) (r(se))
		
	}
		postclose `temp'
	preserve	
		use `tempf', clear
		list
		
		reshape long ideo_, i(var) j(stat) s 
		
		save `tempf', replace
	restore
	
	// Fairness
	tempname temp2
	tempfile tempf2
	postfile `temp2' str20 var favor_coef favor_se using `tempf2'
	forvalues var = 0/3 {
		reg out_fair_bwdiff `reg_sex' `reg_exp' `reg_biden' `reg_distpop' `reg_issues' `int' ///
			cand_age, vce(cluster r_id)
		
		lincom _cons + black_aa`var' 
		post `temp2' ("black_aa`var'") (r(estimate)) (r(se))
		
		lincom _cons + white_aa`var' 
		post `temp2' ("white_aa`var'") (r(estimate)) (r(se))
		
	}
	postclose `temp2'
	
	preserve
		use `tempf2', clear
		list
		
		reshape long favor_, i(var) j(stat) s 
		
		merge 1:1 var stat using `tempf'
		drop _m
		
		gen favor = "0"+string(round(favor_, 0.002))
		replace favor = subinstr(favor, "-","", .)
		replace favor = "-"+favor if substr(string(favor_), 1, 1) == "-"
		//replace favor = "("+favor+")" if stat == "se"
		
		gen ideo = "0"+string(round(ideo_, 0.002))
		replace ideo = subinstr(ideo, "-","", .)
		replace ideo = "-"+ideo if substr(string(ideo_), 1, 1) == "-"
		//replace ideo = "("+ideo+")" if stat == "se"
		
		drop favor_ ideo_
		list
		
		gen order = . 
		replace order = 1 if var == "black_aa0"
		replace order = 2 if var == "white_aa0"
		replace order = 3 if var == "black_aa1"
		replace order = 4 if var == "white_aa1"
		replace order = 5 if var == "black_aa2"
		replace order = 6 if var == "white_aa2"
		replace order = 7 if var == "black_aa3"
		replace order = 8 if var == "white_aa3"
		replace order = order + 0.5 if stat == "se"
		sort order
		
		
		gen varname = ""
		replace varname = "Black X No Position" if order == 1
		replace varname = "White X No Position" if order == 2
		replace varname = "Black X Expand" if order == 3
		replace varname = "White X Expand" if order == 4
		replace varname = "Black X Keep" if order == 5
		replace varname = "White X Keep" if order == 6
		replace varname = "Black X End" if order == 7
		replace varname = "White X End" if order == 8
		
		order var stat varname ideo favor
		drop order var
		
		list
		
		outsheet using table_4.csv, comma replace
	restore
	
// Table 5 (Study 2)
	// Ideology
	rename out_fair_bwdiff out_favor
	foreach var in ideo favor {
		tempname temp_`var'
		tempfile tempf_`var'
		
		postfile `temp_`var'' str20 var `var'_coef `var'_se using `tempf_`var''
		
		reg out_`var' `reg_sex' `reg_exp' `reg_biden' `reg_distpop' `reg_issues' `int' ///
			cand_age, vce(cluster r_id)
		
			lincom black_aa0 - white_aa0
			post `temp_`var'' ("BW_NoPosition") (r(estimate)) (r(se))
			
			lincom black_aa3 - white_aa0
			post `temp_`var'' ("BCons_WNoPos") (r(estimate)) (r(se))
				
			lincom black_aa3 - white_aa3
			post `temp_`var'' ("BW_Cons") (r(estimate)) (r(se))
			
			lincom black_aa1 - white_aa1
			post `temp_`var'' ("BW_Lib") (r(estimate)) (r(se))
			
		postclose `temp_`var''
	}
		
	use `tempf_ideo', clear
	reshape long ideo_, i(var) j(stat) s 
	list
	save `tempf_ideo', replace
	
	
	use `tempf_favor', clear
	reshape long favor_, i(var) j(stat) s
	list
	merge 1:1 var stat using `tempf_ideo'
	drop _m
	list
	
	gen favor = "0"+string(round(favor_, 0.002))
	replace favor = subinstr(favor, "-","", .)
	replace favor = "-"+favor if substr(string(favor_), 1, 1) == "-"
	
	gen ideo = "0"+string(round(ideo_, 0.002))
	replace ideo = subinstr(ideo, "-","", .)
	replace ideo = "-"+ideo if substr(string(ideo_), 1, 1) == "-"
	
	drop favor_ ideo_
	list
	
	gen order = . 
	replace order = 1 if var == "BW_NoPosition"
	replace order = 2 if var == "BCons_WNoPos"
	replace order = 3 if var == "BW_Cons"
	replace order = 4 if var == "BW_Lib"
	replace order = order + 0.5 if stat == "se"
	sort order
	
	
	gen varname = ""
	replace varname = "Black-White, No Position" if order == 1
	replace varname = "Black, Conserative - White, No Position" if order == 2
	replace varname = "Black-White, Conservative" if order == 3
	replace varname = "Black, Liberal - White, Liberal" if order == 4

	
	order var stat varname ideo favor
	drop order var
	
	list
	
	outsheet using table_5.csv, comma replace 
	
